<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
    under one or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information regarding copyright
    ownership. Camunda licenses this file to you under the Apache License,
    Version 2.0; you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.camunda.bpm.engine.impl.persistence.entity.HistoricIncidentEntity">

  <!-- HISTORIC INCIDENT INSERT -->

  <insert id="insertHistoricIncidentEvent" parameterType="org.camunda.bpm.engine.impl.history.event.HistoricIncidentEventEntity">
      insert into ${prefix}ACT_HI_INCIDENT (
        ID_,
        PROC_DEF_KEY_,
        PROC_DEF_ID_,
        ROOT_PROC_INST_ID_,
        PROC_INST_ID_,
        EXECUTION_ID_,
        CREATE_TIME_,
        END_TIME_,
        INCIDENT_MSG_,
        INCIDENT_TYPE_,
        ACTIVITY_ID_,
        FAILED_ACTIVITY_ID_,
        CAUSE_INCIDENT_ID_,
        ROOT_CAUSE_INCIDENT_ID_,
        CONFIGURATION_,
        HISTORY_CONFIGURATION_,
        INCIDENT_STATE_,
        TENANT_ID_,
        JOB_DEF_ID_,
        ANNOTATION_,
        REMOVAL_TIME_
      ) values (
        #{id ,jdbcType=VARCHAR},
        #{processDefinitionKey, jdbcType=VARCHAR},
        #{processDefinitionId, jdbcType=VARCHAR},
        #{rootProcessInstanceId, jdbcType=VARCHAR},
        #{processInstanceId, jdbcType=VARCHAR},
        #{executionId, jdbcType=VARCHAR},
        #{createTime, jdbcType=TIMESTAMP},
        #{endTime, jdbcType=TIMESTAMP},
        #{incidentMessage, jdbcType=VARCHAR},
        #{incidentType, jdbcType=VARCHAR},
        #{activityId ,jdbcType=VARCHAR},
        #{failedActivityId ,jdbcType=VARCHAR},
        #{causeIncidentId ,jdbcType=VARCHAR},
        #{rootCauseIncidentId ,jdbcType=VARCHAR},
        #{configuration, jdbcType=VARCHAR},
        #{historyConfiguration, jdbcType=VARCHAR},
        #{incidentState, jdbcType=INTEGER},
        #{tenantId, jdbcType=VARCHAR},
        #{jobDefinitionId, jdbcType=VARCHAR},
        #{annotation, jdbcType=VARCHAR},
        #{removalTime, jdbcType=TIMESTAMP}
      )
  </insert>

  <!-- HISTORIC INCIDENT UPDATE -->

  <update id="updateHistoricIncidentEvent" parameterType="org.camunda.bpm.engine.impl.history.event.HistoricIncidentEventEntity">
    update
      ${prefix}ACT_HI_INCIDENT
    set
      <!-- MySQL requires timestamps to always be part of the 
      UPDATE statement or else it is going to update them to the current time -->
      CREATE_TIME_ = #{createTime, jdbcType=TIMESTAMP},
      END_TIME_ = #{endTime, jdbcType=TIMESTAMP},
      INCIDENT_STATE_ = #{incidentState, jdbcType=INTEGER},
      PROC_DEF_KEY_ = #{processDefinitionKey, jdbcType=VARCHAR},
      PROC_DEF_ID_ = #{processDefinitionId, jdbcType=VARCHAR},
      EXECUTION_ID_ = #{executionId, jdbcType=VARCHAR},
      ACTIVITY_ID_ = #{activityId, jdbcType=VARCHAR},
      FAILED_ACTIVITY_ID_ = #{failedActivityId, jdbcType=VARCHAR},
      JOB_DEF_ID_ = #{jobDefinitionId, jdbcType=VARCHAR},
      ANNOTATION_ = #{annotation, jdbcType=VARCHAR}
    where
      ID_ = #{id, jdbcType=VARCHAR}
  </update>
  
  <update id="updateHistoricIncident" parameterType="org.camunda.bpm.engine.impl.persistence.entity.HistoricIncidentEntity">
    update
      ${prefix}ACT_HI_INCIDENT
    set
      <!-- MySQL requires timestamps to always be part of the 
      UPDATE statement or else it is going to update them to the current time -->
      CREATE_TIME_ = #{createTime, jdbcType=TIMESTAMP},
      HISTORY_CONFIGURATION_ = #{historyConfiguration, jdbcType=VARCHAR}
    where
      ID_ = #{id, jdbcType=VARCHAR}
  </update>
  
  <update id="updateHistoricIncidentsByRootProcessInstanceId"
          parameterType="java.util.Map">
    update ${prefix}ACT_HI_INCIDENT set
    REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP}

    where ROOT_PROC_INST_ID_ = #{rootProcessInstanceId, jdbcType=VARCHAR}
  </update>

  <update id="updateHistoricIncidentsByRootProcessInstanceId_mssql"
          parameterType="java.util.Map">
    update RES set
    RES.REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP}
    FROM ${prefix}ACT_HI_INCIDENT RES WITH (FORCESEEK)
    where RES.ROOT_PROC_INST_ID_ = #{rootProcessInstanceId, jdbcType=VARCHAR}
  </update>

  <update id="updateHistoricIncidentsByRootProcessInstanceId_mysql"
          parameterType="java.util.Map">
    update ${prefix}ACT_HI_INCIDENT set
    REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP},
    CREATE_TIME_ = CREATE_TIME_

    where ROOT_PROC_INST_ID_ = #{rootProcessInstanceId, jdbcType=VARCHAR}
  </update>

  <update id="updateHistoricIncidentsByProcessInstanceId"
          parameterType="java.util.Map">
    update ${prefix}ACT_HI_INCIDENT
      set REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP}
      where PROC_INST_ID_ = #{processInstanceId, jdbcType=VARCHAR}
  </update>

  <update id="updateHistoricIncidentsByProcessInstanceId_mssql"
          parameterType="java.util.Map">
    update RES set
    RES.REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP}
    FROM ${prefix}ACT_HI_INCIDENT RES WITH (FORCESEEK)
    where PROC_INST_ID_ = #{processInstanceId, jdbcType=VARCHAR}
  </update>

  <update id="updateHistoricIncidentsByProcessInstanceId_mysql"
          parameterType="java.util.Map">
    update ${prefix}ACT_HI_INCIDENT
      set REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP},
          CREATE_TIME_ = CREATE_TIME_
      where PROC_INST_ID_ = #{processInstanceId, jdbcType=VARCHAR}
  </update>

  <update id="updateHistoricIncidentsByBatchId"
          parameterType="java.util.Map">
    update ${prefix}ACT_HI_INCIDENT set
    REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP}

    where ID_ in (SELECT HI.ID_ from ${prefix}ACT_HI_INCIDENT HI
      inner join ${prefix}ACT_HI_BATCH HB
      on HI.JOB_DEF_ID_ in
        (HB.SEED_JOB_DEF_ID_,
         HB.MONITOR_JOB_DEF_ID_,
         HB.BATCH_JOB_DEF_ID_)
      where HB.ID_ = #{batchId})
  </update>

  <update id="updateHistoricIncidentsByBatchId_mssql"
          parameterType="java.util.Map">
    update RES set
    RES.REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP}
    FROM ${prefix}ACT_HI_INCIDENT RES WITH (FORCESEEK)
    where RES.ID_ in (SELECT HI.ID_ from ${prefix}ACT_HI_INCIDENT HI
      inner join ${prefix}ACT_HI_BATCH HB
      on HI.JOB_DEF_ID_ in
        (HB.SEED_JOB_DEF_ID_,
         HB.MONITOR_JOB_DEF_ID_,
         HB.BATCH_JOB_DEF_ID_)
      where HB.ID_ = #{batchId})
  </update>

  <update id="updateHistoricIncidentsByBatchId_mysql"
          parameterType="java.util.Map">
    update ${prefix}ACT_HI_INCIDENT as I
      inner join ${prefix}ACT_HI_INCIDENT as HI
        on HI.ID_ = I.ID_
      inner join ${prefix}ACT_HI_BATCH as HB
        on HI.JOB_DEF_ID_ in
          (HB.SEED_JOB_DEF_ID_,
          HB.MONITOR_JOB_DEF_ID_,
          HB.BATCH_JOB_DEF_ID_)
      set I.REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP},
          I.CREATE_TIME_ = I.CREATE_TIME_
      where HB.ID_ = #{batchId}
  </update>

  <!-- HISTORIC INCIDENT DELETE -->

  <delete id="deleteHistoricIncident" parameterType="org.camunda.bpm.engine.impl.persistence.entity.HistoricIncidentEntity">
    delete from ${prefix}ACT_HI_INCIDENT where ID_ = #{id}
  </delete>

  <delete id="deleteHistoricIncidentsByProcessInstanceIds">
    delete from ${prefix}ACT_HI_INCIDENT
    where
      <bind name="listOfIds" value="list"/>
      <bind name="fieldName" value="'PROC_INST_ID_'"/>
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.applyInForPaginatedCollection"/>
  </delete>

  <delete id="deleteHistoricIncidentsByProcessDefinitionId">
    delete from ${prefix}ACT_HI_INCIDENT where PROC_DEF_ID_ = #{processDefinitionId} and PROC_INST_ID_ is null
  </delete>

  <delete id="deleteHistoricIncidentsByJobDefinitionId">
    delete from ${prefix}ACT_HI_INCIDENT where JOB_DEF_ID_ = #{jobDefinitionId}
  </delete>

  <delete id="deleteHistoricIncidentsByBatchIds">
    delete from ${prefix}ACT_HI_INCIDENT
    where ID_
    in (<include refid="selectHistoricIncidentsByBatchIds" />)
  </delete>

  <delete id="deleteHistoricIncidentsByBatchIds_mysql">
    delete HI.*
    <include refid="selectHistoricIncidentsByBatchIdsSql" />
  </delete>

  <sql id="andWhereMinuteInDateBetweenSql">
    <if test="parameter.minuteFrom != null and parameter.minuteTo != null">
      AND ${datepart1}<include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.periodUnitFunction"/>${datepart2}${date}${datepart3}
      between #{parameter.minuteFrom, jdbcType=INTEGER} and #{parameter.minuteTo, jdbcType=INTEGER}
    </if>
  </sql>

  <sql id="andWhereMinuteInDateBetweenSql_oracle">
    <if test="parameter.minuteFrom != null and parameter.minuteTo != null">
      AND ${datepart1}${date}${datepart2}<include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.periodUnitFunction"/>${datepart3}
      between #{parameter.minuteFrom, jdbcType=INTEGER} and #{parameter.minuteTo, jdbcType=INTEGER}
    </if>
  </sql>

  <delete id="deleteHistoricIncidentsByRemovalTime"
          parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    <bind name="date" value="'REMOVAL_TIME_'"/>
    <bind name="reportPeriodUnitName" value="'MINUTE'"/>
    delete ${limitBeforeWithoutOffset} from ${prefix}ACT_HI_INCIDENT
    where REMOVAL_TIME_ &lt;= #{parameter.removalTime}
    <include refid="andWhereMinuteInDateBetweenSql"/>
    ${limitAfterWithoutOffset}
  </delete>

  <delete id="deleteHistoricIncidentsByRemovalTime_oracle"
          parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    <bind name="date" value="'REMOVAL_TIME_'"/>
    <bind name="reportPeriodUnitName" value="'MINUTE'"/>
    delete ${limitBeforeWithoutOffset} from ${prefix}ACT_HI_INCIDENT
    where REMOVAL_TIME_ &lt;= #{parameter.removalTime}
    <include refid="andWhereMinuteInDateBetweenSql_oracle"/>
    ${limitAfterWithoutOffset}
  </delete>

  <delete id="deleteHistoricIncidentsByRemovalTime_postgres_or_db2"
          parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    <bind name="date" value="'REMOVAL_TIME_'"/>
    <bind name="reportPeriodUnitName" value="'MINUTE'"/>
    delete ${limitBeforeWithoutOffset} from ${prefix}ACT_HI_INCIDENT
    where ID_ IN
      (SELECT ID_
       FROM ${prefix}ACT_HI_INCIDENT
       WHERE REMOVAL_TIME_ &lt;= #{parameter.removalTime} <include refid="andWhereMinuteInDateBetweenSql"/>
       ${limitAfterWithoutOffset})
  </delete>

  <!-- HISTORIC INCIDENT RESULTMAP -->

  <resultMap id="historicIncidentResultMap" type="org.camunda.bpm.engine.impl.persistence.entity.HistoricIncidentEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="processDefinitionKey" column="PROC_DEF_KEY_" jdbcType="VARCHAR" />
    <result property="processDefinitionId" column="PROC_DEF_ID_" jdbcType="VARCHAR" />
    <result property="rootProcessInstanceId" column="ROOT_PROC_INST_ID_" jdbcType="VARCHAR" />
    <result property="processInstanceId" column="PROC_INST_ID_" jdbcType="VARCHAR" />
    <result property="executionId" column="EXECUTION_ID_" jdbcType="VARCHAR" />
    <result property="createTime" column="CREATE_TIME_" jdbcType="TIMESTAMP" />
    <result property="endTime" column="END_TIME_" jdbcType="TIMESTAMP" />
    <result property="incidentMessage" column="INCIDENT_MSG_" jdbcType="VARCHAR" />
    <result property="incidentType" column="INCIDENT_TYPE_" jdbcType="VARCHAR" />
    <result property="activityId" column="ACTIVITY_ID_" jdbcType="VARCHAR" />
    <result property="failedActivityId" column="FAILED_ACTIVITY_ID_" jdbcType="VARCHAR" />
    <result property="causeIncidentId" column="CAUSE_INCIDENT_ID_" jdbcType="VARCHAR" />
    <result property="rootCauseIncidentId" column="ROOT_CAUSE_INCIDENT_ID_" jdbcType="VARCHAR" />
    <result property="configuration" column="CONFIGURATION_" jdbcType="VARCHAR" />
    <result property="historyConfiguration" column="HISTORY_CONFIGURATION_" jdbcType="VARCHAR" />
    <result property="incidentState" column="INCIDENT_STATE_" jdbcType="INTEGER" />
    <result property="tenantId" column="TENANT_ID_" jdbcType="VARCHAR" />
    <result property="jobDefinitionId" column="JOB_DEF_ID_" jdbcType="VARCHAR" />
    <result property="annotation" column="ANNOTATION_" jdbcType="VARCHAR" />
    <result property="removalTime" column="REMOVAL_TIME_" jdbcType="TIMESTAMP"/>
  </resultMap>

  <!-- HISTORIC INCIDENT SELECT -->
  
  <select id="selectHistoricIncidentById" resultMap="historicIncidentResultMap">
    select RES.* from ${prefix}ACT_HI_INCIDENT RES where RES.ID_ = #{id}
  </select>

  <select id="selectHistoricIncidentByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.HistoricIncidentQueryImpl" resultMap="historicIncidentResultMap">
    <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.bindOrderBy"/>
    ${limitBefore}
    select ${distinct} RES.*
    ${limitBetween}
    <include refid="selectHistoricIncidentByQueryCriteriaSql"/>
    ${orderBy}
    ${limitAfter}
  </select>

  <select id="selectHistoricIncidentCountByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.HistoricIncidentQueryImpl" resultType="long">
    ${countDistinctBeforeStart} RES.ID_ ${countDistinctBeforeEnd}
    <include refid="selectHistoricIncidentByQueryCriteriaSql"/>
    ${countDistinctAfterEnd}
  </select>

  <sql id="selectHistoricIncidentByQueryCriteriaSql">
    from ${prefix}ACT_HI_INCIDENT RES

    <if test="authCheck.shouldPerformAuthorizatioCheck &amp;&amp; !authCheck.revokeAuthorizationCheckEnabled &amp;&amp; authCheck.authUserId != null">
      <!--
        HEADS-UP!!!
        If you change something here, you should probably also change it the EE Webapp
        See camunda-bpm-platform-ee/webapps/camunda-webapp/plugins/src/main/resources/org/camunda/bpm/cockpit/impl/plugin/history/historicIncident.xml
      -->
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.authCheckJoinWithoutOnClause"/>
      AUTH ON (AUTH.RESOURCE_ID_ ${authJoinStart}
        RES.PROC_DEF_KEY_ ${authJoinSeparator} '*'
        <if test="authCheck.isHistoricInstancePermissionsEnabled">
          ${authJoinSeparator} RES.PROC_INST_ID_
        </if>
        ${authJoinEnd}
      )
    </if>

    <where>
      <if test="id != null">
        RES.ID_ = #{id}
      </if>
      <if test="incidentType != null">
        and RES.INCIDENT_TYPE_ = #{incidentType}
      </if>
      <if test="incidentMessage != null">
        and RES.INCIDENT_MSG_ = #{incidentMessage}
      </if>
      <if test="incidentMessageLike != null">
        and RES.INCIDENT_MSG_ like #{incidentMessageLike} ESCAPE ${escapeChar}
      </if>
      <if test="executionId != null">
        and RES.EXECUTION_ID_ = #{executionId}
      </if>
      <if test="activityId != null">
        and RES.ACTIVITY_ID_ = #{activityId}
      </if>
      <if test="createTimeAfter != null">
        and RES.CREATE_TIME_ &gt; #{createTimeAfter}
      </if>
      <if test="createTimeBefore != null">
        and RES.CREATE_TIME_ &lt; #{createTimeBefore}
      </if>
      <if test="endTimeAfter != null">
        and RES.END_TIME_ &gt; #{endTimeAfter}
      </if>
      <if test="endTimeBefore != null">
        and RES.END_TIME_ &lt; #{endTimeBefore}
      </if>
      <if test="failedActivityId != null">
        and RES.FAILED_ACTIVITY_ID_ = #{failedActivityId}
      </if>
      <if test="processInstanceId != null">
        and RES.PROC_INST_ID_ = #{processInstanceId}
      </if>
      <if test="processDefinitionId != null">
        and RES.PROC_DEF_ID_ = #{processDefinitionId}
      </if>
      <if test="processDefinitionKey != null">
        and RES.PROC_DEF_KEY_ = #{processDefinitionKey}
      </if>
      <if test="processDefinitionKeys != null &amp;&amp; processDefinitionKeys.length > 0">
        and RES.PROC_DEF_KEY_ in
        <foreach item="item" index="index" collection="processDefinitionKeys"
                 open="(" separator="," close=")">
          #{item}
        </foreach>
      </if>
      <if test="causeIncidentId != null">
        and RES.CAUSE_INCIDENT_ID_ = #{causeIncidentId}
      </if>
      <if test="rootCauseIncidentId != null">
        and RES.ROOT_CAUSE_INCIDENT_ID_ = #{rootCauseIncidentId}
      </if>
      <if test="configuration != null">
        and RES.CONFIGURATION_ = #{configuration}
      </if>
      <if test="incidentState != null">
        and RES.INCIDENT_STATE_ = #{incidentState.stateCode}
      </if>
      <if test="isTenantIdSet">
        <if test="tenantIds != null &amp;&amp; tenantIds.length > 0">
          and RES.TENANT_ID_ in
          <foreach item="tenantId" index="index" collection="tenantIds"
                   open="(" separator="," close=")">
            #{tenantId}
          </foreach>
        </if>
        <if test="tenantIds == null">
          and RES.TENANT_ID_ is null
        </if>
      </if>
      <if test="jobDefinitionIds != null &amp;&amp; jobDefinitionIds.length > 0">
        and RES.JOB_DEF_ID_ in
        <foreach item="jobDefinitionId" index="index" collection="jobDefinitionIds"
                 open="(" separator="," close=")">
          #{jobDefinitionId}
        </foreach>
      </if>

      <if test="authCheck.shouldPerformAuthorizatioCheck &amp;&amp; authCheck.authUserId != null">
        <!--
          HEADS-UP!!!
          If you change something here, you should probably also change it the EE Webapp
          See camunda-bpm-platform-ee/webapps/camunda-webapp/plugins/src/main/resources/org/camunda/bpm/cockpit/impl/plugin/history/historicIncident.xml
        -->
        <!-- incident exists in context of a process definition -->
        and (
          (RES.PROC_DEF_ID_ is not null
          <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.queryAuthorizationCheck"/>
          ) or RES.PROC_DEF_ID_ is null
        )
      </if>

      <include refid="org.camunda.bpm.engine.impl.persistence.entity.TenantEntity.queryTenantCheck" />

    </where>
  </sql>

  <sql id="selectHistoricIncidentsByBatchIds">
    select distinct HI.ID_
    <include refid="selectHistoricIncidentsByBatchIdsSql" />
  </sql>

  <sql id="selectHistoricIncidentsByBatchIdsSql">
    from ${prefix}ACT_HI_INCIDENT HI
    inner join ${prefix}ACT_HI_BATCH HB
    on HI.JOB_DEF_ID_ in
      (HB.SEED_JOB_DEF_ID_,
       HB.MONITOR_JOB_DEF_ID_,
       HB.BATCH_JOB_DEF_ID_)
    where
      <bind name="listOfIds" value="list"/>
      <bind name="fieldName" value="'HB.ID_'"/>
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.applyInForPaginatedCollection"/>
      and HI.JOB_DEF_ID_ is not null
  </sql>

  <select id="selectCompletedHistoricIncidentsPage" resultMap="historicIncidentResultMap" parameterType="java.util.Map" >
    <bind name="maxResults" value="parameter.maxResults" />

    ${optimizeLimitBeforeWithoutOffset}
    select ${limitBeforeWithoutOffset} * from ${prefix}ACT_HI_INCIDENT RES

    <where>
      <if test="parameter.finishedAfter != null">
        and RES.END_TIME_ &gt; #{parameter.finishedAfter}
      </if>
      <if test="parameter.finishedAt != null">
        and RES.END_TIME_ = #{parameter.finishedAt}
      </if>
      <if test="parameter.finishedAfter == null and parameter.finishedAt == null">
        and RES.END_TIME_ is not null
      </if>
    </where>

    ORDER BY RES.END_TIME_ ASC
    ${optimizeLimitAfterWithoutOffset}
  </select>

  <select id="selectOpenHistoricIncidentsPage" resultMap="historicIncidentResultMap" parameterType="java.util.Map">
    <bind name="maxResults" value="parameter.maxResults"/>
    ${optimizeLimitBeforeWithoutOffset}
      select ${limitBeforeWithoutOffset} * from ${prefix}ACT_HI_INCIDENT RES

    <where>
      <if test="parameter.createdAfter != null">
        and RES.CREATE_TIME_ &gt; #{parameter.createdAfter}
      </if>
      <if test="parameter.createdAt != null">
        and RES.CREATE_TIME_ = #{parameter.createdAt}
      </if>
      and RES.END_TIME_ is null
    </where>

    ORDER BY RES.CREATE_TIME_ ASC
    ${optimizeLimitAfterWithoutOffset}
  </select>

</mapper>
